<template>
  <div class="analysis-card">
    <h3 class="analysis-title">直播构成</h3>

    <div class="live-grid">
      <!-- 官号直播 -->
      <div class="live-section">
        <h4 class="section-subtitle">官号</h4>
        <div class="metrics-list">
          <div class="metric-item">
            <div>
              <div class="metric-header">直播成交金额</div>
              <div class="main-value">¥{{ getData('商家直播-直播间成交金额') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('商家直播-直播间成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播间成交金额-较上周期变化') }}
                </div>
                <div class="previous-value">上期: ¥{{ getPreviousValues('商家直播-直播间成交金额', '商家直播-直播间成交金额-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <div class="metric-header">观看人数</div>
              <div class="main-value">{{ getData('商家直播-直播间观看人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('商家直播-直播间观看人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播间观看人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('商家直播-直播间观看人数', '商家直播-直播间观看人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <div class="metric-header">成交券数</div>
              <div class="main-value">{{ getData('商家直播-直播间成交券数') }}张</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('商家直播-直播间成交券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播间成交券数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('商家直播-直播间成交券数', '商家直播-直播间成交券数-较上周期变化') }}张</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">成交人数</div>
              <div class="main-value">{{ getData('商家直播-直播间成交人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('商家直播-直播间成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播间成交人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('商家直播-直播间成交人数', '商家直播-直播间成交人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">直播间转化率</div>
              <div class="main-value">{{ getData('商家直播-直播间成交转化率') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value"
                  :class="getData('商家直播-直播间成交转化率-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播间成交转化率-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('商家直播-直播间成交转化率', '商家直播-直播间成交转化率-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <div class="metric-header">直播时长</div>
              <div class="main-value">{{ getData('商家直播-直播时长') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('商家直播-直播时长-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('商家直播-直播时长-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('商家直播-直播时长', '商家直播-直播时长-较上周期变化') }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 达人直播 -->
      <div class="live-section">
        <h4 class="section-subtitle">达人</h4>
        <div class="metrics-list">
          <div class="metric-item">
            <div>

              <div class="metric-header">直播成交金额</div>
              <div class="main-value">¥{{ getData('达人直播-直播间成交金额') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('达人直播-直播间成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播间成交金额-较上周期变化') }}
                </div>
                <div class="previous-value">上期: ¥{{ getPreviousValues('达人直播-直播间成交金额', '达人直播-直播间成交金额-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">观看人数</div>
              <div class="main-value">{{ getData('达人直播-直播间观看人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('达人直播-直播间观看人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播间观看人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('达人直播-直播间观看人数', '达人直播-直播间观看人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">成交券数</div>
              <div class="main-value">{{ getData('达人直播-直播间成交券数') }}张</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('达人直播-直播间成交券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播间成交券数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('达人直播-直播间成交券数', '达人直播-直播间成交券数-较上周期变化') }}张</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">成交人数</div>
              <div class="main-value">{{ getData('达人直播-直播间成交人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('达人直播-直播间成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播间成交人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('达人直播-直播间成交人数', '达人直播-直播间成交人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">直播间转化率</div>
              <div class="main-value">{{ getData('达人直播-直播间成交转化率') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value"
                  :class="getData('达人直播-直播间成交转化率-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播间成交转化率-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('达人直播-直播间成交转化率', '达人直播-直播间成交转化率-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">直播时长</div>
              <div class="main-value">{{ getData('达人直播-直播时长') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('达人直播-直播时长-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('达人直播-直播时长-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('达人直播-直播时长', '达人直播-直播时长-较上周期变化') }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 店员直播 -->
      <div class="live-section">
        <h4 class="section-subtitle">店员</h4>
        <div class="metrics-list">
          <div class="metric-item">
            <div>

              <div class="metric-header">直播成交金额</div>
              <div class="main-value">¥{{ getData('店员直播-直播间成交金额') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('店员直播-直播间成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播间成交金额-较上周期变化') }}
                </div>
                <div class="previous-value">上期: ¥{{ getPreviousValues('店员直播-直播间成交金额', '店员直播-直播间成交金额-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">观看人数</div>
              <div class="main-value">{{ getData('店员直播-直播间观看人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('店员直播-直播间观看人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播间观看人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('店员直播-直播间观看人数', '店员直播-直播间观看人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">成交券数</div>
              <div class="main-value">{{ getData('店员直播-直播间成交券数') }}张</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('店员直播-直播间成交券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播间成交券数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('店员直播-直播间成交券数', '店员直播-直播间成交券数-较上周期变化') }}张</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">成交人数</div>
              <div class="main-value">{{ getData('店员直播-直播间成交人数') }}人</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('店员直播-直播间成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播间成交人数-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('店员直播-直播间成交人数', '店员直播-直播间成交人数-较上周期变化') }}人</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">直播间转化率</div>
              <div class="main-value">{{ getData('店员直播-直播间成交转化率') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value"
                  :class="getData('店员直播-直播间成交转化率-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播间成交转化率-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('店员直播-直播间成交转化率', '店员直播-直播间成交转化率-较上周期变化') }}</div>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <div class="metric-header">直播时长</div>
              <div class="main-value">{{ getData('店员直播-直播时长') }}</div>
            </div>
            <div class="metric-content">
              <div class="data-comparison">
                <div class="rate-value" :class="getData('店员直播-直播时长-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('店员直播-直播时长-较上周期变化') }}
                </div>
                <div class="previous-value">上期: {{ getPreviousValues('店员直播-直播时长', '店员直播-直播时长-较上周期变化')
                }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getPreviousValue } from '../../../utils'
interface ReportItem {
  name: string
  value: string
}

interface ReportSection {
  title: string
  items: ReportItem[]
}

interface Props {
  sectionData: ReportSection
}
const getPreviousValues = (currentKey: string, changeKey: string,): string => {

  const currentValue = getData(currentKey)
  const changeValue = getData(changeKey)
  return getPreviousValue(currentValue, changeValue)
}
const props = defineProps<Props>()

const getData = (key: string): string => {
  const item = props.sectionData.items.find(item => item.name === key)
  return item?.value ?? '-'
}

</script>

<style scoped>
.analysis-card {
  margin-bottom: 20px;
  padding: 15px 20px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.analysis-title {
  font-size: 18px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 2px solid #e2e8f0;
}

.live-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.live-section {
  background: #f8fafc;
  border-radius: 8px;
  padding: 12px;
}

.section-subtitle {
  font-size: 14px;
  color: #1a365d;
  margin-bottom: 10px;
  padding-left: 8px;
  border-left: 3px solid #4a5568;
}

.metrics-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.metric-item {
  background: #ffffff;
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
}

.metric-header {
  color: #4a5568;
  font-size: 13px;
  margin-bottom: 4px;
  text-align: left;
}

.metric-content {
  display: flex;
  flex-direction: column;
  gap: 6px;
  justify-content: flex-start;
}

.data-comparison {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.main-value {
  font-size: 20px;
  font-weight: 600;
  color: #2d3748;
  line-height: 1;
}

.rate-value {
  font-size: 12px;
  padding: 1px 6px;
  border-radius: 4px;
  line-height: 1.2;
  text-align: center;
}

.previous-value {
  font-size: 11px;
  color: #718096;
  margin-top: 2px;
}

.rate-up {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
}

.rate-down {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

@media (max-width: 768px) {
  .live-grid {
    grid-template-columns: 1fr;
  }
}
</style>